package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.model.User;
import com.example.forum.services.IArticleService;
import com.example.forum.services.IBoradService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;
    @Resource
    private IBoradService boradService;
    //发布新帖子
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @RequestParam(value = "boardId") @NonNull Long boardId,
                           @RequestParam(value = "title") @NonNull String title,
                            @RequestParam(value = "content") @NonNull String content){
        //检查用户是否被禁言
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //版块校验
        Board board = boradService.selectById(boardId.longValue());
        if (board == null || board.getDeleteState() == 1 || board.getState() == 1){
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_BOARD_BANNED);
        }
        //封装文章对象
        Article article = new Article();
        article.setTitle(title);//标题
        article.setContent(content);//正文
        article.setBoardId(boardId);//版块id
        article.setUserId(user.getId());//作者id
        //调用Service
        articleService.create(article);
        //成功
        return AppResult.success();
    }

    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(@RequestParam(value = "boardId", required = false) Long boardId) {
        //定义返回的集合
        List<Article> articles ;
        if (boardId == null){
            //查询所有
            articles = articleService.selectAll();
            if (articles == null){
                articles = new ArrayList<>();
            }
        }else {
            articles = articleService.selectAllByBoardId(boardId);
        }
        if (articles == null){
            articles = new ArrayList<>();
        }

        return AppResult.success(articles);
    }

    //根据帖子id获取..
    @GetMapping("/details")
    public AppResult<Article> getDetails(HttpServletRequest request ,@NonNull Long id){
        //从Session中获取当前用户
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);

        //调用Service
        Article article = articleService.selectDetailById(id);
        //判断是否为空
        if (article == null){
            return AppResult.failed(ResultCode.FAILED);
        }
        //判断当前用户是否为作者
        if (user.getId() == article.getUserId()){
            article.setOwn(true);
        }
        return AppResult.success(article);
    }

    //编辑帖子
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request,@NonNull Long id,@NonNull String title,@NonNull String content){
        //获取当前用户
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        //检验用户是否被禁言
        if (user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //调用Service
        Article article = articleService.selectDetailById(id);
        //判断是否为空
        if (article == null){
            return AppResult.failed(ResultCode.FAILED);
        }
        //判断当前用户是否为作者
        if (user.getId() != article.getUserId()){
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //判断帖子状态
        if (article.getState() == 1 || article.getDeleteState() == 1){
            return AppResult.failed(ResultCode.FAILED);
        }
        //调用Service
        articleService.modify(id,title,content);
        log.info("帖子更新成功 Article id="+id+"user id="+user.getId());
        return AppResult.success();
    }

    //点赞帖子
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,@NonNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户是否被禁言
        if (user.getState() == 1){
            return  AppResult.failed(ResultCode.FAILED);
        }
        //调用Service
        articleService.thumbsUpById(id);
        return AppResult.success();
    }

    //删除帖子
    @PostMapping("/delete")
    public AppResult deleteById (HttpServletRequest request,@NonNull Long id){
        //校验用户状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //查询帖子
        Article article = articleService.selectDetailById(id);
        //判断是否为空
        if (article == null || article.getDeleteState() == 1){
            return AppResult.failed(ResultCode.FAILED);
        }
        if (user.getId() != article.getUserId())
            return AppResult.failed(ResultCode.FAILED);
        //调用Service
        articleService.deleteById(id);
            return AppResult.success();
    }

    //获取用户的帖子列表
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request,@RequestParam(value = "userId",required = false) Long userId){
        //如果userId为空，从Session中获取当前登录的用户Id
        if (userId == null){
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute(AppConfig.USER_SESSION);
            userId = user.getId();
        }
        //调用Service
        List<Article> articles = articleService.selectByUserId(userId);
        return AppResult.success(articles);
    }
}























